package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

@WebServlet("/demo3")
public class ServletDemo3  extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据名称获取数据
        String name = req.getParameter("username");
        System.out.println(name);

        String pwd = req.getParameter("pwd");
        System.out.println(pwd);

        System.out.println("--------------------------");

        String[] hobbies = req.getParameterValues("hobby");
        //遍历
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }

        //获取所有名称
        Enumeration<String> names = req.getParameterNames();
        while (names.hasMoreElements()){
            String value = names.nextElement();
            System.out.println(value);
        }

        //获取所有键值对
        Map<String, String[]> map = req.getParameterMap();
        for (String key : map.keySet()) {
            String[] values = map.get(key);
            System.out.print(key+":");
            for (String value : values) {
                System.out.print(value+" ");
            }
            System.out.println();
        }
    }
}
